﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YYS;

namespace YYSService
{
    public class unifiedUserBase
    {
        /// <summary>
        /// 0 普通 1管理员 8微信
        /// </summary>
        public short root { get; set; }
        public string account { get; set; }
        /// <summary>
        /// 0pc 1小程序 2公众号
        /// </summary>
        public short appId { get; set; }//0pc 1小程序 2公众号
        /// <summary>
        /// 微信小程序openId
        /// </summary>
        public string openId { get; set; }//微信小程序openId
        /// <summary>
        /// 关联用户openId
        /// </summary>
        public string openId_R { get; set; }//关联用户openId
        /// <summary>
        /// 微信公众号openId
        /// </summary>
        public string wx_openId { get; set; }//微信公众号openId
        /// <summary>
        /// 网页登录key
        /// </summary>
        public string openkey { get; set; }

        public string pwd { get; set; }

        public string session_key { get; set; }
        public string state { get; set; }
        public string uiId { get; set; }
        public string uiMark { get; set; }
        public string uiName { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public string uiTime { get; set; }
        /// <summary>
        /// 登录过期时间
        /// </summary>
        public string expiredTime { get; set; }
        /// <summary>
        /// 最近pc活跃时间
        /// </summary>
        public string pcTime { get; set; }
        /// <summary>
        /// 最近微信活跃时间
        /// </summary>
        public string wxTime { get; set; }
        public string imgUrl { get; set; }
        public string email { get; set; }
        public string bucketName { get; set; }
        public string subscriptionsSetting { get; set; }
    }

    /// <summary>
    /// 统一登录用户对象
    /// </summary>
    public class unifiedUser : unifiedUserBase
    {

        /// <summary>
        /// 登录标识
        /// </summary>
        public string key
        {
            get
            {
                var obj = new
                {
                    account = account,
                    pwd = pwd
                };
                return obj.xConvertObjectToJson(false).xAESEncrypt(YYS.DefaultValue.EnvAESKey).xToSafeBase64();//base64 转 URL安全base64safaBase64
            }
        }
        /// <summary>
        /// 请求页面
        /// </summary>
        [Xcode.Json.JsonIgnore]
        [System.Web.Script.Serialization.ScriptIgnore]
        public string page { get; set; }

        public class access
        {
            public int flId { get; set; }
            public int flpId { get; set; }
            public string flName { get; set; }
            public string state { get; set; }
            public string url { get; set; }
            public string color { get; set; }
            public string icon { get; set; }
            public int show { get; set; }
            public int level { get; set; }

            public string wxpage { get; set; }
            public int id { get; set; }
            public int pId { get; set; }
            public string name { get; set; }
            public int platform { get; set; }
            public int badge { get; set; }
            public string page { get; set; }
        }
        /// <summary>
        /// 用户权限菜单 json字符串
        /// </summary>
        public string funjson { get; set; }
        /// <summary>
        /// 用户权限菜单
        /// </summary>
        public List<access> uiAccessList
        {
            get { return funjson.xTrim().Length>0?funjson.xTrim().xConvertJsonToObject<List<access>>().OrderBy(x => x.flId).ToList():new List<access>(); }
        }
        public List<access> wxMenus
        {
            get
            {
                var wxMenus = new List<access>();
                int pId = 0, id = -999 * 100;
                wxMenus.Add(this.addMenu(id--, pId, "userinfo"));
                wxMenus.Add(this.addMenu(id--, pId, "login"));
                foreach (var x in uiAccessList)
                {
                    var obj = this.addMenu(x.flId, x.flpId, x.state, x.flName, x.level, x.icon);
                    if (obj == null) continue;
                    wxMenus.Add(obj);
                }
                if (wx_openId == "o9tiy6R5of_mjjydo23Q55aQp5Do")
                {
                    wxMenus.Add(this.addMenu(503, 500, "envelope"));
                }                
                return wxMenus;
            }
        }
        /// <summary>
        /// 检查教师权限
        /// <para>ids：有全部权限 &amp; 分隔，有任意权限 | 分隔</para>
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool cando(object ids)
        {
            if (this.uiAccessList == null || this.uiAccessList.Count == 0 || ids == null) return false;
            var str = ids.xTrim();
            if (str.Contains("|"))
            {
                return str.Split('|').ToList().Exists(x =>
                {
                    return this.uiAccessList.Exists(y => y.flId.xTrim() == x);
                });
            }
            else
            {
                return !str.Split('&').ToList().Exists(x =>
                {
                    return !this.uiAccessList.Exists(y => y.flId.xTrim() == x);
                });
            }
        }
        #region 验证订阅消息配置项
        public static string getSets(string sets, int idx)
        {
            if (idx <= 0 || idx > 64) return "0";
            var str = sets.xTrim().PadRight(64, '0');
            return str.Substring(idx - 1, 1);
        }

        /// <summary>
        /// 验证订阅消息配置项是否为 0
        /// <para>1：版本更新通知 ；2：日程提醒；</para>
        /// </summary>
        /// <param name="idx"></param>
        /// <returns></returns>
        public bool subscriptionsSets(int idx)
        {
            return unifiedUser.getSets(this.subscriptionsSetting, idx) != "0";
        }
        #endregion

        readonly List<access> Menus = new List<access>()
        { 
            //-------------公共
            new access(){ state = "userinfo", name = "我的服务", level = 4, page="~/Views/wx/userinfo.cshtml", icon = "" },
            new access(){ state = "login", name = "登录", level = 4, page="~/Views/wx/my/login.cshtml", icon = "" },

            new access(){ state = "engine", name = "机油", level = 1 },
            new access(){ state = "engineOil", name = "机油列表", level = 2, page="~/Views/wx/engine/engineOil.cshtml", icon = "" },
            new access(){ state = "bill", name = "账单", level = 2, page="~/Views/wx/engine/bill.cshtml", icon = "" },

            new access(){ state = "my", name = "我的", level = 1 },
            new access(){ state = "fingerPrint", name = "用户", level = 2, page="~/Views/wx/my/fingerPrint.cshtml", icon = "" },
            new access(){ state = "user", name = "个人资料", level = 2, page="~/Views/wx/my/user.cshtml", icon = "" },            
            new access(){ state = "wangEditor", name = "笔记", level = 2, page="~/Views/wx/my/wangEditor.cshtml", icon = "" },
            new access(){ state = "menstruation", name = "姨妈助手", level = 2, page="~/Views/wx/my/menstruation.cshtml", icon = "" },
            new access(){ state = "document", name = "共享空间", level = 2, page="~/Views/wx/my/document.cshtml", icon = "" },

            new access(){ state = "bonus", name = "福利姬", level = 1 },
            new access(){ state = "img", name = "图库", level = 2, page="~/Views/wx/bonus/img.cshtml", icon = "" },
            new access(){ state = "video", name = "视频", level = 2, page="~/Views/wx/bonus/video.cshtml", icon = "" },

            new access(){ state = "app", name = "应用", level = 1 },
            new access(){ state = "schedule", name = "日程", level = 2, page="~/Views/wx/app/schedule.cshtml", icon = "" },
            new access(){ state = "scCACourse", name = "课表", level = 2, page="~/Views/wx/app/scCACourse.cshtml", icon = "" },
            new access(){ state = "envelope", name = "新年红包", level = 2, page="~/Views/wx/app/envelope.cshtml", icon = "lucky-money" },
            #region 管理员菜单
            
            #endregion

            #region 用户菜单
            #endregion
        };
        access addMenu(int id, int pId, string state, string name = null, int? level = null, string icon = null, int? badge = null, string page = null)
        {
            var mx = new access();
            var m = Menus.FirstOrDefault(x => x.state.Equals(state, StringComparison.OrdinalIgnoreCase));
            if (m != null)
            {
                mx = new access()
                {
                    id = id,
                    pId = pId,
                    state = m.state,
                    name = !string.IsNullOrWhiteSpace(m.name) ? m.name : name,
                    level = level.HasValue ? level.Value : m.level,
                    icon = !string.IsNullOrWhiteSpace(m.icon) ? m.icon : icon,
                    badge = badge.HasValue ? badge.Value : m.badge,
                    page = !string.IsNullOrWhiteSpace(page) ? page : m.page
                };
                return mx;
            }
            return null;
        }

    }
}